class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isandroid: !0,
            cid: 1,
            cpid:0,
            sizehtml: [],
            sizeid: '',
            cphnum: 1,
            amount: 835,
            page: 1,
            price: 825,
            sizelist: [
                '160',
                '165',
                '170',
                '175',
                '180',
                '185',
                '190',
                '195',
                '特体',
            ],
            rid: 0,
            cpid:1,
            keyword: '',
            JX: '安卓',
            serverinput: '',
            listhtml: [],
            list: [
            ],
            isall: !1
        };
    }









    componentDidMount() {
        if(Constant.SWITCH_VCONSOLE){
            new VConsole();
        }

        let openid = localStorage.getItem('openid')
        getuser(openid, res => {
            if (res.issuccess == 1) {
                let user = res.user
                this.setState({ user, allprice:835, amount:  835 })
            }
        })

        let sizehtml = [],cphtml=[], { sizelist } = this.state;

        sizehtml.push(<option value={-1}>请选择</option>)
        sizelist.forEach((x, i) => {
            sizehtml.push(
                <option key={i} value={x}>{x} </option>
            )
        })
        cphtml.push(<option value={1}>大包(￥535元)</option>)
        cphtml.push(<option value={0}>小包(￥310元)</option>)
        cphtml.push(<option value={-1}>不购买</option>)

        this.setState({ sizehtml,openid,cphtml })

        //  console.log(liststr?'1':'2','liststr');
        // let list = JSON.parse(localStorage.getItem('orderlist')) || []
        // console.log(list, 'list');

        // this.setState({ list }, () => {
        //     this.getamount()
        // })
        // this.getdata()

    }

    pay(size){
        let{openid,amount} = this.state;
        praypay(openid,Number(amount)*100,size,res=>{
            console.log(res,'resres');
            let data = JSON.parse(res.ret)
            this.requestpay(data)
        })
        
    }

    requestpay(data){
        function onBridgeReady(){
 
         let sign = md5(`appId=wxa29d77bb2e0c40bb&nonceStr=${data.nonce_str}&package=${data.package}&signType=MD5&timeStamp=${data.timeStamp}&key=Y3A7lWq2Xsd7ScCTSp8QUqLiWELxHCl7`).toUpperCase()
             WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                   "appId":"wxa29d77bb2e0c40bb",     //公众号ID，由商户传入     
                   "timeStamp":data.timeStamp,         //时间戳，自1970年以来的秒数     
                   "nonceStr":data.nonce_str, //随机串     
                   "package":data.package,     
                   "signType":"MD5",         //微信签名方式：     
                   "paySign":sign //微信签名 
                },
 
                function(res){
                if(res.err_msg == "get_brand_wcpay_request:ok" ){
                    
                // 使用以上方式判断前端返回,微信团队郑重提示：
                      //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                } 
             }); 
          }
          if (typeof WeixinJSBridge == "undefined"){
             if( document.addEventListener ){
                 document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
             }else if (document.attachEvent){
                 document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
                 document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
             }
          }else{
             onBridgeReady();
          }
     }



    inputbz(e) {
        this.setState({ desc: e.target.value })
    }

    clear() {
        var b = confirm('确定要清空吗')
        if (b) {
            localStorage.removeItem('orderlist')
            this.setState({ list: [], desc: '', amount: 0 })
        }
    }

    gopay() {
        let { sizeid,openid } = this.state;
        if (sizeid == '' || sizeid == '请选择') {
            alert('请先选择尺寸')
        } else {

            canbuy(openid,res=>{
                if(res.data==true){
                    this.pay(sizeid)
                }else{
                    alert('请勿重复购买')
                }

            })
          // 
        }

    }

    selectchange(e) {
        let { value } = e.target
        this.setState({ sizeid: value })
    }

selectchange1(e) {
        let { value } = e.target,{amount,allprice} = this.state;
        if(value==0){
            amount=610
        }if(value==-1){
            amount=300
        }if(value==1){
            amount=835
        }
        this.setState({ cpid: value,amount })
    }


    render() {
        let { list, price, amount, sizehtml, sizeid,cphtml,cpid } = this.state;
        console.log(sizehtml, 'sizehtml');
        return (
            <div id="app">
                <div className="box">

                    <div className="toparea">
                        <div className="top">
                            <img src="./src/pic.png" alt="" />
                        </div>
                    </div>

                    <div className="word">
                        <div className="d1">
                            其它费用共计：￥{amount}
                        </div>
                       
                        <div className="desc">
                            <div className="title">军训服（80元/套）</div>
                            <div className="w1">
                                领取事宜：请学生凭微信/支付宝缴费凭证领取军训服;
                                各学院根据学校安排的时间到指定地点领取
                            </div>
                            <div className="w1">
                                军训服套装包括：
                            </div>
                            <div className="w1">

                            迷彩服（上衣、裤子），迷彩帽，内腰带，外腰带
                            </div>
                            {/* <div className="w1">
                                管信学院（85元/套）：迷彩服（上衣、裤子），迷彩帽，迷彩T恤，内腰带，外腰带
                            </div> */}
                            <div className="title mt20">
                                床上用品
                            </div>
                            <div className="w1 size">
                                <div>选择床上用品</div>
                                <select value={cpid} onChange={this.selectchange1.bind(this)}>{cphtml}</select>
                            </div>
                            <img src="./src/123.png" alt="" />

                            {/* {
                                cpid==1?
                                <div className="w1">
                               棉被1床，棉褥1床，枕头1个，被罩2个，床单2个，枕套2个，枕巾2个，毛巾被1床，蚊帐1个，包装袋（大）1个，手提式收纳袋(蓝)1个，床垫1个，不锈钢暖瓶1个，脸盆2个，红色提袋1个。
                            </div>
                            :cpid==0?
                            <div className="w1">
                            被罩2个，床单2个，枕套2个，枕巾2个，毛巾被1床，蚊帐1个，手提式收纳袋(蓝)1个，床垫1个，红色提袋1个。
                        </div>:null
                            } */}
                            
                           
                            <div className="title mt20">
                            实训服（170元/套）
                            </div>
                            <div className="w1 size">
                                <div>选择尺寸</div>
                                <select value={sizeid} onChange={this.selectchange.bind(this)}>{sizehtml}</select>
                            </div>
                            <div className="w1">
                                <img src="./src/bg.png" alt="" />
                            </div>
                         

                            <div className="w1">
                                备注：请学生参考尺码对照表填写适合自己的衣服尺码，身高与体重无法对应的情况建议以对应大号的尺码进行填写。
                            </div>
                            <div className="w1">
                                特体需另外量体，具体事宜听学校通知。
                            </div>
                            <div className="title mt20">
                                体检费（50元）
                            </div>
                          
                        </div>
                    </div>





                    {/* <div className="content">
                        <div className="header">
                            <span>菜品明细</span>
                            <span onClick={this.del.bind(this)}>删除</span>
                        </div>
                        <div className="goodslist">
                            {
                                list.map((x,i)=>{
                                    return(
                                        <div className="goods">
                                        <div className="left">
                                            <img className="selimg" onClick={this.sel.bind(this, i)} src={`./src/radio${x.issel ?'_c':''}.png`} />
                                            <img className="goodimg" src={x.imageUrl} alt="" />
                                            <div className="name">{x.foodName}</div>
                                        </div>
                                        <div className="countbox">
                                            <span className="price">￥{x.price}</span>
                                            <img src="./src/reduce.png" alt="" onClick={this.setnum.bind(this,'reduce',i)} />
                                            <span className="num">{x.num}</span>
                                            <img src="./src/add.png" alt="" onClick={this.setnum.bind(this,'add',i)} />
                                        </div>
                                    </div>
                                    )    
                                
                                })
                            }
                        </div>
                    </div> */}
                </div>

                <div onClick={this.gopay.bind(this)} className="bottom">
                    <p className="price">支付金额：￥{amount || 0}</p>
                    {/* <p onClick={this.gopay.bind(this)} className="btn">去支付</p> */}

                    {/* <div className="bottom_l">
                        <img onClick={this.setall.bind(this)} src={`./src/radio${isall ?'_c':''}.png`} alt="" />
                        <span>全选</span>
                    </div>
                    <div className="bottom_r">
                        <span className="hj">合计：</span>
                        <span className="price">￥{amount}</span>
                        <div className="subbtn">结算</div>
                    </div> */}

                </div>
            </div>
        )
    }
}

ReactDOM.render(
    <div>
        {/* <TopLine /> */}
        <div id='body'>
            {/* <Tabbar index='1' /> */}
            <Main />
        </div>
    </div>
    // <Barrage />
    , $('.container')[0]
)